package dk.ihk.ssgroup5.service;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import dk.ihk.ssgroup5.dto.Database;
import dk.ihk.ssgroup5.dto.User;

public class LoginService {	
	
	public User getUserDetails(String username) throws UnsupportedEncodingException {
		
		
		String sql = "select * from users where username = ?";

		Connection con = new Database().newCon();
		User user = new User();
		
		PreparedStatement ps;
		ResultSet rs;
		
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			rs.next();
			user.setUserId(rs.getInt("id"));
			user.setUserName(rs.getString("username"));			
			user.setUserType(rs.getInt("type"));
			user.setUserEmail(rs.getString("email"));	
		} catch (SQLException sx) {
			System.out.println(sx);
		} finally {
			try {
				con.close();
			} catch (Throwable ignore) {
			}
		}

		return user;

	}

	public boolean authenticate(String username, String password) {

		try {
			try {
				if ((username == null) || (password == null)) {
					return false;
				}
				else if ((!allowUser(username, password))){
					return false;
				}

				else { 
					return true;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return false;		
	}
	

	
	protected boolean allowUser(String username, String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		
		
		String sql = "select * from users where username = ?";
		Connection con = new Database().newCon();
		
	
		PreparedStatement ps = null;
		ResultSet rs = null;			
				
		try {
						
			ps = con.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			if(!rs.next()){
				return false;
			}
			String dbhpassword = rs.getString("password");
			String hpassword = SHA256(password);
			if(!dbhpassword.equals(hpassword)){
				return false;
			}
			else{
				return true;
			}
			
		} catch (SQLException sx) {
			System.out.println(sx);
		} finally {
			try {
				con.close();
			} catch (Throwable ignore) {
			}
		}		
		return false;
	}
	

	private String SHA256(String text) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
		MessageDigest md;
		md = MessageDigest.getInstance("SHA-256");
		byte[] sha256hash = new byte[40];
		md.update(text.getBytes("UTF8"), 0, text.length());
		sha256hash = md.digest();
		return convertToHex(sha256hash);
	}

	private String convertToHex(byte[] data) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < data.length; i++) {
			int halfbyte = (data[i] >>> 4) & 0x0F;
			int two_halfs = 0;
			do {
				if ((0 <= halfbyte) && (halfbyte <= 9))
					buf.append((char) ('0' + halfbyte));
				else
					buf.append((char) ('a' + (halfbyte - 10)));
				halfbyte = data[i] & 0x0F;
			} while (two_halfs++ < 1);
		}
		return buf.toString();
	}
	
}
